package com.giscloud.amp.service.auth.impl;

import com.giscloud.amp.dao.AmpStaffMenuDataMapper;
import com.giscloud.amp.entity.AmpStaffMenuData;
import com.giscloud.amp.entity.dto.AmpStaffMenuDataDTO;
import com.giscloud.amp.entity.vo.AmpStaffMenuDataVo;
import com.giscloud.amp.service.auth.AmpStaffMenuDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:gxd
 * @Description: 权限服务类
 * @Date: 17:19 2018/2/24
 * @Modified By:
 */
@Service("ampStaffMenuDataService")
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public class AmpStaffMenuDataServiceImpl implements AmpStaffMenuDataService {
    private final static Logger log = LoggerFactory.getLogger(AmpStaffMenuDataServiceImpl.class);
    @Autowired
    private AmpStaffMenuDataMapper ampStaffMenuDataMapper;


     @Override
    public int saveStaffMenuData(AmpStaffMenuDataDTO ampStaffMenuDataDTO) {
        AmpStaffMenuData ampStaffMenuData = new AmpStaffMenuData();
        BeanUtils.copyProperties(ampStaffMenuDataDTO,ampStaffMenuData);
        ampStaffMenuData.setCreatedDt(new Date());
        ampStaffMenuData.setVersion(new Date());
        ampStaffMenuData.setDataType(2);
        int i = 0;
        for (Long menuId:ampStaffMenuDataDTO.getMenuAuth().keySet()){
            ampStaffMenuData.setMenuId(menuId);
            Map<String,List<Long>> authMap = ampStaffMenuDataDTO.getMenuAuth().get(menuId);
            if(authMap.get("ckAuthIds")!=null){
                List<Long> ckAuthIds = authMap.get("ckAuthIds");
                for(Long authId:ckAuthIds){
                    ampStaffMenuData.setAuthId(authId);
                    i+=insert(ampStaffMenuData);
                }
            }
            if(authMap.get("unkAuthIds")!=null){
                List<Long> unkAuthIds = authMap.get("unkAuthIds");
                for(Long authId:unkAuthIds){
                    ampStaffMenuData.setAuthId(authId);
                    i+=delete(ampStaffMenuData);
                }
            }
        }
        return i;
    }

   @Override
    public List<AmpStaffMenuDataVo> findAuthDataByMenuId(AmpStaffMenuDataDTO ampStaffMenuDataDTO) {
        AmpStaffMenuData ampStaffMenuData = new AmpStaffMenuData();
        BeanUtils.copyProperties(ampStaffMenuDataDTO,ampStaffMenuData);
        return ampStaffMenuDataMapper.findAuthDataByMenuId(ampStaffMenuDataDTO.getMenuIds(),ampStaffMenuData);
    }


    @Override
    public int delete(AmpStaffMenuData ampStaffMenuData){
        ampStaffMenuData.setStatusCd(2);
        return ampStaffMenuDataMapper.delete(ampStaffMenuData);
    }

    @Override
    public int insert(AmpStaffMenuData ampStaffMenuData){
        int i = 0;
        ampStaffMenuData.setStatusCd(1);
        if(ampStaffMenuDataMapper.findAmpStaffMenuDataCountByAuthId(ampStaffMenuData)==0) {
            i+=ampStaffMenuDataMapper.insert(ampStaffMenuData);
        }
        return i;
    }

}
